package LeetCode刷题.暑假刷的算法;

import java.util.Scanner;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-09-08 19:13
 * @description: Functions of this class is
 * 请编写一个函数，使其可以删除某个链表中给定的（非末尾）节点。传入函数的唯一参数为 要被删除的节点 。
 **/
public class 删除链表中的节点 {
    public static class ListNode {
        int val;
        ListNode next;
        public ListNode(){

        }
        public ListNode(int x){
            this.val=x;
        }
    }

    public static void main(String args[]) {
        ListNode head=new ListNode();
        head=createList(head);
        print(head);
        deleteNode(head,new ListNode(3));
//        deleteNode(head,new ListNode(1));
//        deleteNode(head,new ListNode(5));
        print(head);
    }
    private static ListNode createList(ListNode head){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        ListNode p;
        while (n-->0){
            p=new ListNode();
            p.val=scanner.nextInt();
            p.next=head.next;
            head.next=p;
        }
        return head;
    }
    public static void print(ListNode head){
        ListNode p=head.next;
        while (p!=null){
            System.out.print(p.val+" ");
            p=p.next;
        }
        System.out.println();
    }
    public static void deleteNode(ListNode head,ListNode node) {
       while (head.next!=null){
           if(head.val==node.val){
               //找到了要删除的节点
               head.val=head.next.val;
               head.next=head.next.next;
               break;
           }
       }
    }

}